import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_widgets/resources/images.dart';
import '../../resources/kq_theme_colors.dart';

import '../../config/kq_global.dart';
import '../../theme/kq_theme_manager.dart';
import '../divider/kq_divider.dart';
import 'entity/kq_form_entity.dart';

class KqChooseView extends StatefulWidget {
  const KqChooseView(
      {Key? key,
      required this.data,
      this.cancel = false,
      this.callBack,
      this.forbidActionBlock})
      : super(key: key);
  final bool cancel;
  final KqFormEntity
      data; //extraData : [{"title":xx,"titleId":xx,"selectedImg":xx,"normalImg":xx,"nonOperateImg":xx,"forbid":xx}]
  final dynamic callBack;
  final Function? forbidActionBlock;

  @override
  State<KqChooseView> createState() => _KqChooseViewState();
}

class _KqChooseViewState extends State<KqChooseView> {

  @override
  void initState() {
    widget.data.widgetState = this;
    super.initState();
  }

  //左侧的必填
  Widget renderStarView() {
    var star = widget.data.mustInput == true ? '∗' : '';
    return SizedBox(
      width: 16.r,
      child: Text(
        star,
        textAlign: TextAlign.center,
        style: TextStyle(
            color: Colors.red,
            fontSize: KqThemeManager.instance
                    .getConfig()
                    .formConfig
                    .mustInputFontSize ??
                14.sp),
      ),
    );
  }

  //title
  Widget renderTitleView() {
    return SizedBox(
      width: 104.r,
      child: Text(
        widget.data.title,
        maxLines: 1,
        style: TextStyle(
            color: KqThemeColors.text26,
            fontSize:
                KqThemeManager.instance.getConfig().formConfig.titleFontSize ??
                    16.sp),
      ),
    );
  }

  //选择的
  Widget renderRadio(Map item) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        renderIcon(item),
        Padding(
          padding: EdgeInsets.only(left: 5.r),
          child: Text(
            item["title"] ?? '',
            style: TextStyle(
                fontSize: KqThemeManager.instance
                        .getConfig()
                        .formConfig
                        .contentFontSize ??
                    16.sp,
                color: KqThemeColors.text59),
          ),
        )
      ],
    );
  }

  Widget renderIcon(Map item) {
    if (item['forbid'] != null) {
      bool forbid = item['forbid'] == '1' ? true : false;
      if (forbid) {
        return Image.asset(
          item["nonOperateImg"] ?? Images.commonIcIncreaseNormal,
          width: 16.r,
          height: 16.r,
          package: item["selectedImg"] != null ? null : KqGlobal.packageName,
        );
      }
    }
    if (widget.data.valueForSubmit != null) {
      //存在提交参数
      if (widget.data.valueForSubmit == item['titleId']) {
        //是这个数据
        return Image.asset(
          item["selectedImg"] ?? Images.commonIcChoice16CSel,
          width: 16.r,
          height: 16.r,
          package: item["selectedImg"] != null ? null : KqGlobal.packageName,
        );
      }
    }
    return Image.asset(
      item["normalImg"] ?? Images.commonIcChoice16Nor,
      width: 16.r,
      height: 16.r,
      package: item["selectedImg"] != null ? null : KqGlobal.packageName,
    );
  }

//选择
  Widget renderSelect(Map item) {
    return InkWell(
      onTap: () {
        if (item['forbid'] != null) {
          bool forbid = item['forbid'] == '1' ? true : false;
          if (forbid) {
            if (widget.forbidActionBlock != null) {
              widget.forbidActionBlock!();
            }
            return;
          }
        }
        setState(() {
          if (widget.cancel) {
            if (widget.data.valueForSubmit == item["titleId"]) {
              //当前这个 取消
              widget.data.valueForSubmit = '';
              widget.data.valueForShow = '';
              if (widget.callBack != null) {
                widget.callBack();
              }
              return;
            }
          }
          widget.data.valueForSubmit = item["titleId"];
          widget.data.valueForShow = item["title"];
          if (widget.callBack != null) {
            widget.callBack();
          }
        });
      },
      child: Container(
          height: 44.r,
          padding: EdgeInsets.only(left: 8.r),
          child: renderRadio(item)),
    );
  }

//选择
  List<Widget> renderChoose() {
    List<Widget> listView = [];
    widget.data.extraData.forEach((item) => {listView.add(renderSelect(item))});
    return listView;
  }

  @override
  Widget build(BuildContext context) {
    // 如果外层实体是反复重新创建的，则需要在此重新赋值
    widget.data.widgetState = this;
    return Container(
      color: KqThemeColors.bgWhite,
      child: Column(
        children: [
          Row(
            children: [
              renderStarView(),
              renderTitleView(),
              Expanded(
                child: Padding(
                  padding: EdgeInsets.only(right: 16.r),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: renderChoose(),
                  ),
                ),
              )
            ],
          ),
          const KqDivider()
        ],
      ),
    );
  }
}
